package auctionhaus

import grails.test.mixin.TestFor

/**
 * Created by IntelliJ IDEA.
 * User: Todd
 * Date: 2/6/12
 * Time: 8:28 PM
 * To change this template use File | Settings | File Templates.
 */
@TestFor(Listing)
class ListingTests extends GroovyTestCase {

    void testNameIsRequired() {
        def listing = new Listing()
        assert !listing.validate()
        assert 'nullable' == listing.errors['name'].code
    }

    void testEndDateTimeIsRequired() {
        def listing = new Listing()
        assert !listing.validate()
        assert 'nullable' == listing.errors['endDateTime'].code
    }

    void testStartingBidAmountIsRequired() {
        def listing = new Listing()
        assert !listing.validate()
        assert 'nullable' == listing.errors['startingBidAmount'].code
    }

    void testSellerIsRequired() {
        def listing = new Listing()
        assert !listing.validate()
        assert 'nullable' == listing.errors['seller'].code
    }

    void testHasBids() {
        def listing = new Listing()
        listing.addToBids(new Bid())
        assert listing.getBids().size() == 1
    }

    void testDescriptionIsOptional() {
        def listing = new Listing()
        listing.validate()
        assert null == listing.errors['description']
    }

    void testWinnerIsOptional() {
        def listing = new Listing()
        listing.validate()
        assert null == listing.errors['winner']
    }

    void testEndDateTimeInFuture() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        assert listing.validate()
        assert null == listing.errors['endDateTime']
    }

    void testEndDateTimeInPast() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def listing = new Listing(name: 'bicycle', endDateTime: new Date() - 1, seller: alice, startingBidAmount: 100)
        assert !listing.validate()
        assert 'notinfuture' == listing.errors['endDateTime'].code
    }

    void testDescriptionTooLong() {
        def desc = new String().padLeft(256, 'a')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: desc)
        assert !l.validate()
        assert 'size.toobig' == l.errors['description'].code
    }

    void testDescriptionNotTooLong() {
        def desc = new String().padLeft(255, 'a')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: desc)
        l.validate()
        assert null == l.errors['description']
    }

    void testDescriptionEmpty() {
        def desc = new String('')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: desc)
        l.validate()
        assert null == l.errors['description']
    }

    void testNameTooLong() {
        def nm = new String().padLeft(64, 'a')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: nm, endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: 'description')
        assert !l.validate()
        assert 'size.toobig' == l.errors['name'].code
    }

    void testNameNotTooLong() {
        def nm = new String().padLeft(63, 'a')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: nm, endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: 'description')
        l.validate()
        assert null == l.errors['name']
    }

    void testNameOneCharacter() {
        def nm = new String('a')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: nm, endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: 'description')
        l.validate()
        assert null == l.errors['name']
    }

    void testNameEmpty() {
        def nm = new String('')
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        def l = new Listing(name: nm, endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100, description: 'description')
        l.validate()
        assert 'blank' == l.errors['name'].code
    }
}